package com.pimee.service.admin.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.DateUtil;
import com.pimee.mapper.ext.ExtSysDictTypeMapper;
import com.pimee.model.SysDictType;
import com.pimee.model.SysUser;
import com.pimee.model.web.Ztree;
import com.pimee.service.BaseService;
import com.pimee.service.admin.ISysDictTypeService;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

@Slf4j
@Service
public class SysDictTypeService extends BaseService<SysDictType> implements ISysDictTypeService {

	@Autowired
	private ExtSysDictTypeMapper extSysDictTypeMapper;
	
	@Override
	public PageInfo<SysDictType> pageList(Map<String, Object> params) {
		log.info("===> 字典类型列表分页查询...");
		startPage(params);// 设置分页
		List<SysDictType> list = extSysDictTypeMapper.pageList(params);
		return new PageInfo<>(list);
	}

	@Override
	public void insertDictType(SysDictType param) {
		Date now = DateUtil.now();
		param.setCreateTime(now);
		param.setUpdateTime(now);
		this.saveNotNull(param);
	}

	@Override
	public void updateDictType(SysDictType param) {
		param.setUpdateTime(DateUtil.now());
		this.updateNotNull(param);
	}

	@Override
	public SysDictType selectDictTypeByType(String dictType) {
		Example example = new Example(SysUser.class);
    	example.createCriteria().andEqualTo("dictType", dictType);
		return this.selectOneByExample(example);
	}

	/**
     * 查询字典类型树
     * 
     * @param dictType 字典类型
     * @return 所有字典类型
     */
	@Override
	public List<Ztree> selectDictTree() {
        List<Ztree> ztrees = new ArrayList<>();
        List<SysDictType> dictList = this.listAll();
        for (SysDictType dict : dictList){
            if ("1".equals(dict.getStatus())) {
                Ztree ztree = new Ztree();
                ztree.setId(dict.getDictId());
                ztree.setName(transDictName(dict));
                ztree.setTitle(dict.getDictType());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    public String transDictName(SysDictType dictType) {
        StringBuffer sb = new StringBuffer();
        sb.append("(" + dictType.getDictName() + ")");
        sb.append("&nbsp;&nbsp;&nbsp;" + dictType.getDictType());
        return sb.toString();
    }

}
